<?php

include_once dirname(dirname(__FILE__)) . '/include/common.php';
$core = new core();
$customer_order_id = isset($_POST["customer_order_id"]) ? intval($_POST["customer_order_id"]) : 0;
$status = isset($_POST["status"]) ? intval($_POST["status"]) : 0;
$zen_customer_order = $core->getZenCustomerOrderById($customer_order_id);
if ($zen_customer_order) {
    if ($status >= 0 && $status < 9) {
        $reload_data = 0;
        $core->db->query("UPDATE zen_customer_order SET customer_order_status=$status
                            WHERE customer_order_id=" . $customer_order_id);
//        if ($status == 6) {
//            $reload_data = 1;
//            //het hang, product price =0, update total_current_money,total_order_money
//
//            $core->db->query("UPDATE zen_customer_order SET total_order_money=0,
//                        total_current_money= $zen_customer_order->total_payment,customer_order_status=$status
//                            WHERE customer_order_id=" . $customer_order_id);
//            $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,product_price=0
//             WHERE customer_order_id=$customer_order_id");
//        } else {
////            
//            $reload_data = 1;
//            //chuyen trang thai ra khoi trang thai het hang, update lai product price, total..
//            $customer_order_details = $core->db->get_results("SELECT zcod.id,zcod.order_status,zcod.product_num,zcod.product_price as zcod_product_price,zp.product_price 
//                                                                FROM zen_customer_order_detail zcod, zen_product zp 
//                                                                WHERE zcod.product_id=zp.product_id AND zcod.customer_order_id=$customer_order_id AND zcod.order_status !=6");
//            $total_order_money = 0;
//            if ($customer_order_details) {
//                foreach ($customer_order_details as $detail) {
//                    $total_order_money = $total_order_money + ($detail->product_num * $detail->zcod_product_price);
//                    switch ($status) {
//                        case 0:
//                            $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,store_date = now() WHERE customer_order_id=$customer_order_id AND order_status !=6 ");
//                            break;
//                        case 1:
//                            $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,online_date = now() WHERE customer_order_id=$customer_order_id AND order_status !=6");
//
//                            break;
//                        case 3:
//                            $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,received_date = now() WHERE customer_order_id=$customer_order_id AND order_status !=6");
//
//                            break;
//                        case 5:
//                            $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,saled_date = now() WHERE customer_order_id=$customer_order_id AND order_status !=6");
//
//                            break;
//                        case 7:
//                            $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status,customer_order_date = now() WHERE customer_order_id=$customer_order_id AND order_status !=6");
//
//                            break;
//                        default :
//                            $core->db->query("UPDATE zen_customer_order_detail SET order_status=$status WHERE customer_order_id=$customer_order_id AND order_status !=6");
//
//                            break;
//                    }
//                }
//                $total_current_money = $zen_customer_order->total_payment - $total_order_money;
//                $core->db->query("UPDATE zen_customer_order SET total_order_money=$total_order_money,
//                        total_current_money= $total_current_money,customer_order_status=$status
//                            WHERE customer_order_id=" . $customer_order_id);
//            }
 //       }

        echo json_encode(array('type' => 0, 'msg' => "update trạng thái thành công!",
            'reload_data' => $reload_data, 'customer_order_id' => $customer_order_id
        ));
        die;
    } else {
        echo json_encode(array('type' => 1, 'msg' => "update trạng thái thất bại!"));
        die;
    }
} else {
    echo json_encode(array('type' => 1, 'msg' => "update trạng thái thất bại!"));
    die;
}
?>